from datetime import datetime
from typing import List
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_stock.entity.do.daily_do import StockDaily
from module_stock.service.daily_service import DailyService
from module_stock.entity.vo.daily_vo import DeleteDailyModel, DailyModel, DailyPageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


dailyController = APIRouter(prefix='/stock/daily', dependencies=[Depends(LoginService.get_current_user)])

@dailyController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('stock:daily:list'))]
)
async def get_stock_daily_list(
    request: Request,
daily_page_query: DailyPageQueryModel = Depends(DailyPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    daily_page_query_result = await DailyService.get_daily_list_services(query_db, daily_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=daily_page_query_result)

@dailyController.get(
    '/all',
    response_model=List[DailyModel],
    dependencies=[Depends(CheckUserInterfaceAuth('stock:daily:all'))],
)
async def get_all_stock_daily(request: Request, symbol: str, query_db: AsyncSession = Depends(get_db)):
    # 获取全量数据
    daily_list_result = await DailyService.get_all_daily_services(query_db, symbol)
    logger.info('获取成功')

    return ResponseUtil.success(data=daily_list_result)


@dailyController.post('', dependencies=[Depends(CheckUserInterfaceAuth('stock:daily:add'))])
@ValidateFields(validate_model='add_daily')
@Log(title='股票日线', business_type=BusinessType.INSERT)
async def add_stock_daily(
    request: Request,
    add_daily: DailyModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_daily.create_time = datetime.now()
    add_daily.update_time = datetime.now()
    add_daily_result = await DailyService.add_daily_services(query_db, add_daily)
    logger.info(add_daily_result.message)

    return ResponseUtil.success(msg=add_daily_result.message)


@dailyController.put('', dependencies=[Depends(CheckUserInterfaceAuth('stock:daily:edit'))])
@ValidateFields(validate_model='edit_daily')
@Log(title='股票日线', business_type=BusinessType.UPDATE)
async def edit_stock_daily(
    request: Request,
    edit_daily: DailyModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_daily.update_by = current_user.user.user_name
    edit_daily.update_time = datetime.now()
    edit_daily_result = await DailyService.edit_daily_services(query_db, edit_daily)
    logger.info(edit_daily_result.message)

    return ResponseUtil.success(msg=edit_daily_result.message)


@dailyController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('stock:daily:remove'))])
@Log(title='股票日线', business_type=BusinessType.DELETE)
async def delete_stock_daily(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_daily = DeleteDailyModel(ids=ids)
    delete_daily_result = await DailyService.delete_daily_services(query_db, delete_daily)
    logger.info(delete_daily_result.message)

    return ResponseUtil.success(msg=delete_daily_result.message)


@dailyController.get(
    '/{id}', response_model=DailyModel, dependencies=[Depends(CheckUserInterfaceAuth('stock:daily:query'))]
)
async def query_detail_stock_daily(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    daily_detail_result = await DailyService.daily_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=daily_detail_result)


@dailyController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('stock:daily:export'))])
@Log(title='股票日线', business_type=BusinessType.EXPORT)
async def export_stock_daily_list(
    request: Request,
    daily_page_query: DailyPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    daily_query_result = await DailyService.get_daily_list_services(query_db, daily_page_query, is_page=False)
    daily_export_result = await DailyService.export_daily_list_services(daily_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(daily_export_result))
